﻿using System;
using System.Data;
using System.Web.Security;
using Framework;

namespace Enterprise.Admin.Handle
{
    /// <summary>
    /// 用户登入
    /// </summary>
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var userName = Fq("userName");
            var password = Fq("password");
            var code = Fq("code");
            if (string.IsNullOrEmpty(userName.NullSafe().Trim()))
                Json(new { state = false, message = "用户名不能为空" });
            if (string.IsNullOrEmpty(password.NullSafe().Trim()))
                Json(new { state = false, message = "密码不能为空" });
            if (string.IsNullOrEmpty(code.NullSafe().Trim()))
                Json(new { state = false, message = "验证码为空" });
            if (Session["LoginCodeSessionKey"] != null && code.ToLower() != Session["LoginCodeSessionKey"].ToString().ToLower())
                Json(new { state = false, message = "验证码输入不正确,请重输" });
            var adminService = new AdminService();
            if (adminService.Login(userName, password))
            {
                FormsAuthentication.SetAuthCookie(userName, true);
                Json(new { state = true, message = "登入成功", url = FormsAuthentication.GetRedirectUrl(userName, true) });
            }
            else
            {
                Session["code"] = null;
                Json(new { state = false, message = "用户名或者密码错误" });
            }

        }
    }
}